package com.yjhealth.libs.core.webinterface.client;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class YjhealthCoreWebViewClient extends WebViewClient {
    private CoreClientInterface clientInterface;

    public YjhealthCoreWebViewClient(CoreClientInterface clientInterface) {
        this.clientInterface = clientInterface;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (clientInterface != null) {
            return clientInterface.shouldOverrideUrlLoading(view, url);
        }else {
            return super.shouldOverrideUrlLoading(view, url);
        }
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        if (clientInterface != null) {
            clientInterface.onPageStarted(view, url, favicon);
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if (clientInterface != null) {
            clientInterface.onPageFinished(view, url);
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        if (clientInterface != null) {
            clientInterface.onReceivedError(view, errorCode, description, failingUrl);
        }
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        if (clientInterface != null) {
            clientInterface.onReceivedSslError(view, handler, error);
        }
    }
}
